<?php
namespace app\admin\controller;



use think\Db;
use think\Controller;

class Supply  extends  Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('supply');
            $dc = Db::name('dc');
            $biz = Db::name('biz');

            $res = $gd->page($page, $limit)
                ->order('id', 'desc')
                ->select();
            foreach ($res as $index => $re) {
                $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
                $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
                $res[$index]['name1']=$dc->where('id', $res[$index]['dc_id'])->value('name');
                $res[$index]['name2']=$biz->where('id', $res[$index]['biz_id'])->value('name');
            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('supply')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }



    public function edit()
    {

        if (request()->isAjax()) {
            $data = input('');
            if (!$data) {
                $this->error('请将信息填写完整');
            }
            $biz=Db::name('biz')->where('name',$data['alias'])->value('id');
            $data['biz_id']=$biz;
            $dc=Db::name('dc')->where('name',$data['alias_address'])->value('id');
            $data['dc_id']=$dc;

            $data['updatetime']=time();
            $res = Db::name('supply')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('supply')->find($id);
            $res['biz']=Db::name('biz')->where('id', $res['biz_id'])->value('name');
            $res['dc']=Db::name('dc')->where('id', $res['dc_id'])->value('name');



            $res1=Db::name('biz')->field('name')->select();
            $res2=Db::name('dc')->field('name')->select();
            $this->assign('sj', $res1);
            $this->assign('dc', $res2 );

            $this->assign('item', $res);
            return $this->fetch();
        }


    }


    public function add()
    {

        if (request()->isAjax()) {
            $data = input('');
            if (!$data['alias']) {
                $this->error('请选择商家商户');
            }
            if (!$data['alias_address']) {
                $this->error('请选择配送中心');
            }
            $biz=Db::name('biz')->where('name',$data['alias'])->value('id');
            $data['biz_id']=$biz;
            $dc=Db::name('dc')->where('name',$data['alias_address'])->value('id');
            $data['dc_id']=$dc;
            $data['createtime']=time();
            $res = Db::name('supply')->insertGetId($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            $res1=Db::name('biz')->field('name')->select();
            $res2=Db::name('dc')->field('name')->select();
            $this->assign('sj', $res1);
            $this->assign('dc', $res2 );
            return $this->fetch();
        }


    }



    //删除操作
    public function del(){
        $data = input('');
        $ids = $data['ids'];
        foreach ($ids as $id) {
            $res = Db::name('supply')->where('id', $id)->delete();
        }
        if ($res) {
            $this->success('操作成功');
        }
    }



    public function base_so()
    {
        $status[0] = '禁用';
        $status[1] = '正常';
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }

        $res = Db::name('supply')->page($page, $limit)
           ->join('fa_biz', 'fa_biz.id=fa_supply.biz_id')
           ->join('fa_dc', 'fa_dc.id=fa_supply.dc_id')
           ->field('fa_biz.name  as name2 ,fa_dc.name  as  name1,fa_supply.createtime,fa_supply.updatetime,fa_supply.status')
            //搜索相关
            //参数绑定
            // ->where('id > :id AND name LIKE :name ', ['id' => 0, 'name' => 'thinkphp%'])
            ->where('fa_dc.name LIKE :key1 OR fa_biz.name LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
            ->select();

        foreach ($res as $index => $re) {
            $res[$index]['createtime'] = date('Y-m-d H:i:s', $re['createtime']);
            $res[$index]['updatetime'] = date('Y-m-d H:i:s', $re['updatetime']);
        }
        //根据关键字返回 相应数据
        $total =
            Db::name('supply')->page($page, $limit)
                ->join('fa_biz', 'fa_biz.id=fa_supply.biz_id')
                ->join('fa_dc', 'fa_dc.id=fa_supply.dc_id')
                ->field('fa_biz.name  as name2 ,fa_dc.name  as  name1,fa_supply.createtime,fa_supply.updatetime,fa_supply.status')
                //搜索相关
                //参数绑定
                // ->where('id > :id AND name LIKE :name ', ['id' => 0, 'name' => 'thinkphp%'])
                ->where('fa_dc.name LIKE :key1 OR fa_biz.name LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
                ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
    }


    public function select_biz()
    {
        if ($this->request->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('biz')->field('name')->page($page, $limit)->order('id', 'desc')->select();
            $this->success('ok', '', $res);
        } else {

            $count = Db::name('biz')->field('name')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }


    public function select()
    {

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 6;
        $flag = input('flag');
        $key = trim(input('keyword'));
        $data_type = input('data_type');
        if (!$key) {
            $this->error('没有内容');
        }
            if ($data_type == "count") {
                //根据关键字返回 相应数据
                $total = Db::name('biz')
                    ->where(' name LIKE :key ', ['key' => "%$key%"])
                    ->count();
                $res = -1;
            } else {
                $res = Db::name('biz')
                    ->where(' name LIKE :key ', ['key' => "%$key%"])
                    ->page($page, $limit)
                    ->select();
                $total = -1;
            }


//        } else {
//            $item = '';
//            if ($flag == 1) {
//                $item = "is_pm";
//            }
//            if ($flag == 2) {
//                $item = "is_am";
//            }
//            if ($flag == 3) {
//                $item = "is_ds";
//            }
//            if ($flag == 4) {
//                $item = "is_qc";
//            }
//            if ($data_type == "count") {
//                //根据关键字返回 相应数据
//                $total = Db::name('biz')
//                    ->where('name LIKE :key ', ['key' => "%$key%"])
//                    ->where($item, 1)
//                    ->count();
//                $res = -1;
//            } else {
//                $res = Db::name('biz')
//                    ->where('name LIKE :key ', ['key' => "%$key%"])
//                    ->where($item, 1)
//                    ->page($page, $limit)
//                    ->select();
//                $total = -1;
//            }
//
//
//        }

        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

    }
    public function select_dc()
    {
        if ($this->request->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('dc')->field('name')->page($page, $limit)->order('id', 'desc')->select();
            $this->success('ok', '', $res);
        } else {

            $count = Db::name('dc')->field('name')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }


    public function selectdc()
    {

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 6;
        $flag = input('flag');
        $key = trim(input('keyword'));
        $data_type = input('data_type');
        if (!$key) {
            $this->error('没有内容');
        }
        if ($data_type == "count") {
            //根据关键字返回 相应数据
            $total = Db::name('dc')
                ->where(' name LIKE :key ', ['key' => "%$key%"])
                ->count();
            $res = -1;
        } else {
            $res = Db::name('dc')
                ->where(' name LIKE :key ', ['key' => "%$key%"])
                ->page($page, $limit)
                ->select();
            $total = -1;
        }

        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

    }


}
